package cn.edu.scut.sys.dao.impl;

import java.sql.Timestamp;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.sql.JoinType;
import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.ShootScoreDao;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.ShootScore;

@Component("shootScoreDao")
public class ShootScoreDaoImpl implements ShootScoreDao{

	private SessionFactory sessionFactory;

	@Resource(name="sessionFactory")
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Long count() {
		String hql = "select count(id) from ShootScore";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		return Long.valueOf(query.uniqueResult().toString());
	}

	@Override
	public List<ShootScore> list(int start, int limit, String sort, String sidx,
			String unitByNowGroupId, String unitByNewTeamId, String unitByNowClassId,
			String fromDate, String toDate, String studentName) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ShootScore.class);
		criteria.createAlias("student", "student", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowGroup", "unitByNowGroup", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNewTeam", "unitByNewTeam", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowClass", "unitByNowClass", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("leader", "leader", JoinType.LEFT_OUTER_JOIN);
		
		if(!StringUtils.isEmpty(unitByNowGroupId)){
			criteria.add(Restrictions.eq("unitByNowGroup.id", Long.parseLong(unitByNowGroupId)));
		}
		if(!StringUtils.isEmpty(unitByNewTeamId)){
			criteria.add(Restrictions.eq("unitByNewTeam.id", Long.parseLong(unitByNewTeamId)));
		}
		if(!StringUtils.isEmpty(unitByNowClassId)){
			criteria.add(Restrictions.eq("unitByNowClass.id", Long.parseLong(unitByNowClassId)));
		}
		
		if(!StringUtils.isEmpty(fromDate)){
			criteria.add(Restrictions.ge("trainDate", Timestamp.valueOf(fromDate + " 00:00:00")));
		}
		
		if(!StringUtils.isEmpty(toDate)){
			criteria.add(Restrictions.le("trainDate", Timestamp.valueOf(toDate + " 00:00:00")));
		}
		
		if(!StringUtils.isEmpty(studentName)){
			criteria.add(Restrictions.ilike("student.name", studentName, MatchMode.ANYWHERE));
		}
		
		criteria.setFirstResult(start).setMaxResults(limit);
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(sidx)){
			if ("desc".equals(sort)){
				criteria.addOrder(Order.desc(sidx));
			} else if ("asc".equals(sort)){
				criteria.addOrder(Order.asc(sidx));
			}
		}
		
		return criteria.list(); 
	}

	@Override
	public void insert(ShootScore shootScore) {
		sessionFactory.getCurrentSession().save(shootScore);
		sessionFactory.getCurrentSession().flush();
		sessionFactory.getCurrentSession().clear();
	}

	@Override
	public ShootScore get(Long id) {
		return (ShootScore)sessionFactory.getCurrentSession().load(ShootScore.class, id);
	}

	@Override
	public void update(ShootScore shootScore) {
		sessionFactory.getCurrentSession().update(shootScore);
		sessionFactory.getCurrentSession().flush();
		sessionFactory.getCurrentSession().clear();
	}

	@Override
	public boolean del(int id) {
	
			String hql = "delete ShootScore u where u.id = ?";
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, id);
	 
			return (query.executeUpdate() > 0);

	}
	@Override
	public List<ShootScore> list(String unitByNowGroupId, String unitByNewTeamId) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ShootScore.class);
		criteria.createAlias("student", "student", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowGroup", "unitByNowGroup", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNewTeam", "unitByNewTeam", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowClass", "unitByNowClass", JoinType.LEFT_OUTER_JOIN);
		
		if(!StringUtils.isEmpty(unitByNowGroupId)){
			criteria.add(Restrictions.eq("unitByNowGroup.id", Long.parseLong(unitByNowGroupId)));
		}
		if(!StringUtils.isEmpty(unitByNewTeamId)){
			criteria.add(Restrictions.eq("unitByNewTeam.id", Long.parseLong(unitByNewTeamId)));
		}
		return criteria.list();
	}

	@Override
	public List<ShootScore> analyze(int start, int limit, String sort,
			String sidx, String unitByNowGroupId, String unitByNewTeamId,
			String unitByNowClassId, String fromDate, String toDate,
			String studentName) {
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(ShootScore.class);
		criteria.createAlias("student", "student", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowGroup", "unitByNowGroup", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNewTeam", "unitByNewTeam", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("student.unitByNowClass", "unitByNowClass", JoinType.LEFT_OUTER_JOIN);
		criteria.createAlias("leader", "leader", JoinType.LEFT_OUTER_JOIN);
		
		if(!StringUtils.isEmpty(unitByNowGroupId)){
			criteria.add(Restrictions.eq("unitByNowGroup.id", Long.parseLong(unitByNowGroupId)));
		}
		if(!StringUtils.isEmpty(unitByNewTeamId)){
			criteria.add(Restrictions.eq("unitByNewTeam.id", Long.parseLong(unitByNewTeamId)));
		}
		if(!StringUtils.isEmpty(unitByNowClassId)){
			criteria.add(Restrictions.eq("unitByNowClass.id", Long.parseLong(unitByNowClassId)));
		}
		
		if(!StringUtils.isEmpty(fromDate)){
			criteria.add(Restrictions.ge("trainDate", Timestamp.valueOf(fromDate + " 00:00:00")));
		}
		
		if(!StringUtils.isEmpty(toDate)){
			criteria.add(Restrictions.le("trainDate", Timestamp.valueOf(toDate + " 00:00:00")));
		}
		
		if(!StringUtils.isEmpty(studentName)){
			criteria.add(Restrictions.ilike("student.name", studentName, MatchMode.ANYWHERE));
		}
		criteria.add(Restrictions.or(
				Restrictions.or(Restrictions.or(Restrictions.ge("stopTime1", 8),Restrictions.ge("stopTime2", 8)),Restrictions.ge("stopTime3", 8)),
				Restrictions.or(Restrictions.or(Restrictions.le("fixHit", 0),Restrictions.le("moveHit", 0)),Restrictions.le("gunHit", 0))));
	
		criteria.setFirstResult(start).setMaxResults(limit);
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(sidx)){
			if ("desc".equals(sort)){
				criteria.addOrder(Order.desc(sidx));
			} else if ("asc".equals(sort)){
				criteria.addOrder(Order.asc(sidx));
			}
		}
		
		return criteria.list(); 
	}
	
	
}
